package org.babygo.connect.handler.server.msg;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;

import org.babygo.connect.NettyFileOperator;
import org.babygo.connect.client.NettyClient;
import org.babygo.connect.protocol.GetAndSendFileRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ServerFileSendOutHandler extends ChannelOutboundHandlerAdapter{
	private Logger log = LoggerFactory.getLogger(NettyClient.class);

	@Override
	public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
		if (msg instanceof GetAndSendFileRequest){
			log.info("正在发送文件！");
			GetAndSendFileRequest getAndSendFileRequest = (GetAndSendFileRequest)msg;
			log.info("告知客户端准备把文件:" + getAndSendFileRequest.getFileName()+ "发送给客户端");
			NettyFileOperator.readyToSendFiles.put(getAndSendFileRequest.getFileName(), getAndSendFileRequest.getFile());
			getAndSendFileRequest.setFile(null);
			ctx.writeAndFlush(getAndSendFileRequest, promise);
		} else log.info("未知操作");
	}

}